package com.acme.bankapp.service.bank;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import com.acme.bankapp.domain.bank.Bank;

public class BankServer {
	static final int PORT = 2014;

	public static void main(String[] args) {
		Bank bank = new Bank();
		int i = 0;
		System.out.println("Server started");
		try (ServerSocket listener = new ServerSocket(PORT)) {
			while (true) {
				if (i > 5) {
					bank = new Bank();
					i = 0;
				}
				i++;
				try (Socket client = listener.accept();
						BufferedReader reader = new BufferedReader(
								new InputStreamReader(client.getInputStream()));) {

					System.out.println("Connection accepted");

					BankDataLoaderService.inetLoadFeed(bank, reader);
					System.out.println("Connection closed");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
	}
}
